package dao;

import java.sql.*;
import java.util.*;
import to.OrdenPedidoTO;
import to.DetallePedidoTO;

public class OrdenPedidoDAO extends GenericDAO{
    public OrdenPedidoDAO(Transaction transacction) {
	super(transacction);
    }
    
    @SuppressWarnings("unchecked")
    public List<OrdenPedidoTO> listarPedidos(OrdenPedidoTO to) throws TransactionException {
        Connection conn;
        List<OrdenPedidoTO> list = new ArrayList();
        try {
            int i = 1;
            conn = (Connection) t.getConnection();
            String sqlCall = "{call uspListarPedidos(?,?,?,?,?)}";
            CallableStatement procedure = conn.prepareCall(sqlCall);
            procedure.setString(i++, to.getNOrdenPedido());
            procedure.setString(i++, to.getCCliente());
            procedure.setString(i++, to.getFOrdenPedido());
            procedure.setString(i++, to.getFOrdenPedido());
            procedure.setString(i++, to.getCEstado());
            ResultSet rs = procedure.executeQuery();

            while (rs.next()) {
                int j=0;
                OrdenPedidoTO vo = new OrdenPedidoTO(); 
                vo.setNOrdenPedido(rs.getString(++j));
                vo.setDCliente(rs.getString(++j));
                vo.setFOrdenPedido(rs.getString(++j));
                vo.setCMoneda(rs.getString(++j));
                vo.setITotal(rs.getDouble(++j));
                vo.setCUsuarioCreo(rs.getString(++j));
                vo.setDEstado(rs.getString(++j));
                list.add(vo);
            }

            procedure.close();
            conn.close();

        } catch (SQLException e1) {
                throw new TransactionException(e1);
        }
        return list;
    }
    
    public String selSeriePedido(String tip) throws Exception {
        Connection conn;
        String result = "";
        try {
            int i = 1;
            conn = (Connection) t.getConnection();
            String sqlCall = "{call uspSelSerie(?,?)}";
            CallableStatement procedure = conn.prepareCall(sqlCall);
            procedure.setString(i++, tip);
            procedure.registerOutParameter(i++, java.sql.Types.CHAR);
            procedure.execute();
            result = procedure.getString("RETURNVALUE");
            procedure.close();
            conn.close();
        } catch (SQLException e1) {
            throw new TransactionException(e1);
        }
        return result;
    }
    
    public String guardaNuevo(OrdenPedidoTO to) throws Exception {
        Connection conn;
        String result = "";
        try {
            int i = 1;
            conn = (Connection) t.getConnection();
            String sqlCall = "{call uspInsCabeceraPedido(?,?,?,?,?,?,?,?,?,?)}";
            CallableStatement procedure = conn.prepareCall(sqlCall);
            procedure.setString(i++, to.getCCliente());
            procedure.setString(i++, to.getCEstado());
            procedure.setString(i++, to.getCMoneda());
            procedure.setDouble(i++, to.getIVenta());
            procedure.setDouble(i++, to.getIDescuento());
            procedure.setDouble(i++, to.getIImpuesto());
            procedure.setDouble(i++, to.getITotal());
            procedure.setString(i++, to.getDObservaciones());
            procedure.setString(i++, to.getCUsuarioCreo());
            procedure.setString(i++, to.getDIpCreacion());
            ResultSet rs = procedure.executeQuery();
            
            while (rs.next()) {
                int j=0;
                result = rs.getString(++j);
            }
            
            procedure.close();
            conn.close();
        } catch (SQLException e1) {
            throw new TransactionException(e1);
        }
        return result;
    }
    
    public void guardaDetalle(DetallePedidoTO to, OrdenPedidoTO orden, String codAlmacen) throws TransactionException {
        Connection conn;
        try {
            int i = 1;
            conn = (Connection) t.getConnection();
            String sqlCall = "{call uspInsDetallePedido(?,?,?,?,?,?,?,?)}";
            CallableStatement procedure = conn.prepareCall(sqlCall);
            procedure.setString(i++, orden.getNOrdenPedido());
            procedure.setInt(i++, to.getNItem());
            procedure.setString(i++, to.getCProducto());
            procedure.setDouble(i++, to.getIPrecio());
            procedure.setDouble(i++, to.getNCantidad());
            procedure.setDouble(i++, to.getIDescuento());
            procedure.setDouble(i++, to.getIVenta());
            procedure.setString(i++, codAlmacen);
            procedure.execute();
            procedure.close();
            conn.close();
        } catch (SQLException e1) {
            throw new TransactionException(e1);
        }
    }
    
    public void anularPedido(String ordenPedido) throws Exception {
        Connection conn;
        try {
            int i = 1;
            conn = (Connection) t.getConnection();
            String sqlCall = "{call uspAnulaPedido(?)}";
            CallableStatement procedure = conn.prepareCall(sqlCall);
            procedure.setString(i++, ordenPedido);
            procedure.execute();
            procedure.close();
            conn.close();
        } catch (SQLException e1) {
            throw new TransactionException(e1);
        }
    }
}